package com.jojo.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jojo.domain.Order;
import com.jojo.model.Result;
import com.jojo.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 订单业务模块控制层
 */
@Api("订单业务接口管理")
@RequestMapping("order/order")
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("多条件分表查询订单")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('order:order:page')")
    public Result<Page<Order>> loadOrderPage(@RequestParam Long current,
                                             @RequestParam Long size,
                                             @RequestParam(required = false) String orderNumber,
                                             @RequestParam(required = false) Integer status,
                                             @RequestParam(required = false) Date startTime,
                                             @RequestParam(required = false) Date endTime){
        Page<Order> page = new Page<>(current,size);
        page = orderService.queryOrderPage(page,orderNumber,status,startTime,endTime);
        return Result.success(page);
    }

    @ApiOperation("根据订单编号查询订单详情")
    @GetMapping("orderInfo/{orderNumber}")
    @PreAuthorize("hasAuthority('order:order:info')")
    public Result<Order> loadOrderDetail(@PathVariable Long orderNumber){
        Order order = orderService.queryOrderDetailByOrderNumber(orderNumber);
        return Result.success(order);
    }

    @ApiOperation("导出销售记录")
    @GetMapping("soldExcel")
    @PreAuthorize("hasAuthority('order:order:soldExcel')")
    public Result<?> exportSoldOrderRecordExcel(){
        //查询所有销售记录
        List<Order> list = orderService.list(new LambdaQueryWrapper<Order>()
                .orderByDesc(Order::getCreateTime)
        );
        String fileName = "D:\\" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName,Order.class).sheet("订单").doWrite(list);
        return Result.success(null);
    }

}
